# Plotting OFC
plotofc <- function(exppar=r2n11, natp=512, nptp=64, ps=F){
# arguments:
#  exppar: object name of experiment results, a list
#  natp  : number of total possible test points
#  nptp  : size of test points population
#  ps    : whether or not output to a ps file
#
    # Set data object
    out0401 <- r2n11.ofc$co.as
    out0402 <- r2n11.ofc$co.ws
    out0405 <- r2n11.ofc$co.ai
    out0406 <- r2n11.ofc$co.wi
    # objf    
    o0401 <- apply(r2n11.objf$co.as,1,mean)
    o0402 <- apply(r2n11.objf$co.ws,1,mean)
    o0405 <- apply(r2n11.objf$co.ai,1,mean)
    o0406 <- apply(r2n11.objf$co.wi,1,mean)
    # subjf    
    s0401 <- apply(r2n11.subjf$co.as,1,mean)
    s0402 <- apply(r2n11.subjf$co.ws,1,mean)
    s0405 <- apply(r2n11.subjf$co.ai,1,mean)
    s0406 <- apply(r2n11.subjf$co.wi,1,mean)
    # ofc
    c0401 <- apply(r2n11.ofc$co.as,1,mean)
    c0402 <- apply(r2n11.ofc$co.ws,1,mean)
    c0405 <- apply(r2n11.ofc$co.ai,1,mean)
    c0406 <- apply(r2n11.ofc$co.wi,1,mean)
    
    # Indexing
    ratio = natp / nptp
    ng0301 <- length(o0401)%/%ratio
    print(paste(ratio,ng0301))
    idx2 <- seq(ratio,length(o0401),by=1)

    # Set output
    if(ps){
        ofile <- paste(deparse(substitute(exppar)),".ofc.ps",sep="")
        print(paste("Output to",ofile))
        postscript(ofile)
    }

    # Plotting paramters
    par(mfrow=c(3,2))

    # 0401
    plot(c0401,ylim=c(-1,1),type="l",lwd=2,lty=1,col=1,
         xlab="Generation",ylab="")
    lines(s0401,type="l",lwd=1,lty=2,col=2)
    lines(o0401,type="l",lwd=1,lty=3,col=3)
    for(i in 4:4){
        lines(out0401[,i],type="l",lwd=1,lty=i,col=i)
    }
    title("coevolution / average-score")

    # 0402
    plot(c0402,ylim=c(-1,1),type="l",lwd=2,lty=1,col=1,
         xlab="Generation",ylab="")
    lines(s0402,type="l",lwd=1,lty=2,col=2)
    lines(o0402,type="l",lwd=1,lty=2,col=2)
    for(i in 4:4){
        lines(out0402[,i],type="l",lwd=1,lty=i,col=i)
    }
    title("coevolution / weighted-score")

    # 0405
    plot(c0405,ylim=c(-1,1),type="l",lwd=2,lty=1,col=1,
         xlab="Generation",ylab="")
    lines(s0405,type="l",lwd=1,lty=2,col=2)
    lines(o0405,type="l",lwd=1,lty=2,col=2)
    for(i in 4:4){
        lines(out0405[,i],type="l",lwd=1,lty=i,col=i)
    }
    title("coevolution / average-info")

    # 0406
    plot(c0406,ylim=c(-1,1),type="l",lwd=2,lty=1,col=1,
         xlab="Generation",ylab="")
    lines(s0406,type="l",lwd=1,lty=2,col=2)
    lines(o0406,type="l",lwd=1,lty=2,col=2)
    for(i in 4:4){
        lines(out0406[,i],type="l",lwd=1,lty=i,col=i)
    }
    title("coevolution / weighted-infoe")

    # Main trend
    #plot(c0301[1:ng0301],ylim=c(-1,1),type="l",lty=1,
    #     xlab=paste("Interaction (*",natp,")"),ylab="Best Fitness")
    plot(c0401[idx2],ylim=c(0,1),type="l",lty=1,col=1,
         xlab="Generation",ylab="OFC")
    #lines(c0401[idx2],type="l",lty=2,col=2)
    lines(c0402[idx2],type="l",lty=3,col=3)
    lines(c0405[idx2],type="l",lty=5,col=5)
    lines(c0406[idx2],type="l",lty=6,col=6)
    # Legend
    #lposx <- ng0301/3
    #legend(lposx,0.3,ncol=3,
    #       c("0301","0401","0402","0405","0406"),
    #       lty=c(1,2,3,5,6),
    #       col=c(1,2,3,5,6),
    #       cex=0.8)
    title("Averaged OFC")
    
    # Legend
    plot.new()
    #rect(0,0,1,1)
    legend(0.1,0.9,ncol=2,
           c("Ave-OFC","Ave-SubjF","Ave-ObjF","OFC-run1"),
           lwd=c(2,1,1,1),
           lty=c(1,2,3,4),
           col=c(1,2,3,4),
           cex=0.8)
    legend(0.1,0.3,ncol=2,
           c("average-score","weighted-score","average-info","wrighted-info"),
           lty=c(1,3,5,6),
           col=c(1,3,5,6),
           cex=0.8)
    
    # Closing device and clean up
    if(ps)
        dev.off()
}
